package org.boot.ssm.wxshop.service.impl;

import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.Collections;
import org.boot.ssm.wxshop.mapper.GoodsMapper;
import org.boot.ssm.wxshop.service.CollectionService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: wxshop
 * @description: 用户收藏业务实现
 * @author: 刘涛
 * @create: 2018-03-23 14:43
 **/
@Service
public class CollectionServiceImpl extends BaseServiceImpl<Collections> implements CollectionService {

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 添加评论
     * @param collections
     * @return JsonResult
     */
    @Override
    @Transactional
    public JsonResult userCollection(Collections collections) {
        Collections setCollections = this.queryByParam(collections);
        if(setCollections==null){
            this.saveNoNull(collections);
            goodsMapper.updateCollectionNum(collections.getGoodsid());
            return new JsonResult(200,"收藏成功");
        }else {
            if(setCollections.getIddelete()==1){
                setCollections.setIddelete(0);
                this.updateNoNull(setCollections);
                goodsMapper.updateCollectionNum(collections.getGoodsid());
                return new JsonResult(500,"取消收藏");
            }else {
                setCollections.setIddelete(1);
                this.updateNoNull(setCollections);
                goodsMapper.updateCollectionNum(collections.getGoodsid());
                return new JsonResult(200,"收藏成功");
            }
        }
    }
}
